// =====================================================
// Article API 服务
// =====================================================

import request from "../utils/request";
import type {
  Article,
  PaginatedArticles,
  ArticleQuery,
  ArticleStats,
} from "../types";

// 获取文章列表
export const getArticleList = (params: ArticleQuery) => {
  return request.get<any, PaginatedArticles>("/articles", { params });
};

// 获取文章详情
export const getArticle = (id: number) => {
  return request.get<any, Article>(`/articles/${id}`);
};

// 标记文章为已读
export const markAsRead = (id: number) => {
  return request.post<any, void>(`/articles/${id}/read`);
};

// 标记文章为未读
export const markAsUnread = (id: number) => {
  return request.post<any, void>(`/articles/${id}/unread`);
};

// 批量标记已读
export const batchMarkAsRead = (ids: number[]) => {
  return request.post<any, void>("/articles/batch/read", { ids });
};

// 切换收藏状态
export const toggleFavorite = (id: number) => {
  return request.post<any, Article>(`/articles/${id}/favorite`);
};

// 更新笔记
export const updateNote = (id: number, note: string) => {
  return request.put<any, Article>(`/articles/${id}/note`, { note });
};

// 删除文章
export const deleteArticle = (id: number) => {
  return request.delete<any, void>(`/articles/${id}`);
};

// 批量删除文章
export const batchDeleteArticles = (ids: number[]) => {
  return request.post<any, void>("/articles/batch/delete", { ids });
};

// 搜索文章
export const searchArticles = (keyword: string, page = 1, size = 20) => {
  return request.get<any, PaginatedArticles>("/articles/search", {
    params: { keyword, page, size },
  });
};

// 获取文章统计
export const getArticleStats = () => {
  return request.get<any, ArticleStats>("/articles/stats");
};

// 导出 Markdown
export const exportMarkdown = (params: {
  start_date: string;
  end_date: string;
}) => {
  return request.post<any, { content: string; filename: string }>(
    "/articles/export",
    params
  );
};
